package com.shioirikukuri.controller;

import com.shioirikukuri.entity.UserMemberLevel;
import com.shioirikukuri.service.IUserMemberLevelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/admin/user-member-levels")
public class UserMemberLevelController {
	@Autowired
	private IUserMemberLevelService userMemberLevelService;


	/**
	 * 创建会员等级
	 *
	 * @param userMemberLevel 会员等级信息
	 * @return 创建成功的会员等级
	 */
	@PostMapping
	public ResponseEntity<UserMemberLevel> createUserMemberLevel(@RequestBody UserMemberLevel userMemberLevel) {
		if (!validateUserMemberLevel(userMemberLevel)) {
			System.err.println("Invalid input data: " + userMemberLevel);
			return new ResponseEntity<>(HttpStatus.BAD_REQUEST); // 返回400状态码表示请求无效
		}
		userMemberLevelService.createUserMemberLevel(userMemberLevel);
		return new ResponseEntity<>(userMemberLevel, HttpStatus.CREATED);
	}

	/**
	 * 查询所有会员等级
	 *
	 * @return 会员等级列表
	 */
	@GetMapping
	public ResponseEntity<List<UserMemberLevel>> getAllUserMemberLevels() {
		List<UserMemberLevel> userMemberLevels = userMemberLevelService.getAllUserMemberLevels();
		return ResponseEntity.ok(userMemberLevels);
	}

	/**
	 * 根据ID查询会员等级
	 *
	 * @param id 会员等级ID
	 * @return 会员等级信息
	 */
	@GetMapping("/{id}")
	public ResponseEntity<UserMemberLevel> getUserMemberLevelById(@PathVariable Long id) {
		UserMemberLevel userMemberLevel = userMemberLevelService.getUserMemberLevelById(id);
		return ResponseEntity.ok(userMemberLevel);
	}

	/**
	 * 更新会员等级信息
	 *
	 * @param id 会员等级ID
	 * @param userMemberLevel 会员等级信息
	 * @return 更新后的会员等级
	 */
	@PutMapping("/{id}")
	public ResponseEntity<UserMemberLevel> updateUserMemberLevel(@PathVariable Long id, @RequestBody UserMemberLevel userMemberLevel) {
		userMemberLevel.setId(id);
		userMemberLevelService.updateUserMemberLevel(userMemberLevel);
		return ResponseEntity.ok(userMemberLevel);
	}

	/**
	 * 根据ID删除会员等级
	 *
	 * @param id 会员等级ID
	 * @return 删除成功响应
	 */
	@DeleteMapping("/{id}")
	public ResponseEntity<Void> deleteUserMemberLevelById(@PathVariable Long id) {
		userMemberLevelService.deleteUserMemberLevelById(id);
		return ResponseEntity.noContent().build();
	}


	private boolean validateUserMemberLevel(UserMemberLevel userMemberLevel) {
		if (userMemberLevel.getName() == null || userMemberLevel.getName().trim().isEmpty()) {
			System.err.println("Name cannot be empty or null.");
			return false; // 名称不能为空
		}
		if (userMemberLevel.getPointsRequired() != null && userMemberLevel.getPointsRequired() < 0) {
			System.err.println("Points required cannot be negative.");
			return false; // 升级所需积分不能为负数
		}
		if (userMemberLevel.getPointsExchangeRate() != null && userMemberLevel.getPointsExchangeRate() < 0) {
			System.err.println("Points exchange rate cannot be negative.");
			return false; // 积分兑换比例不能为负数
		}
		return true;
	}
}